# -*- coding: utf-8 -*-
#Player class

import pyglet

from constants import *
from objects import *

class Player(GameObject):
    def __init__(self, image, x, y, batch=None):
        self.sprite = pyglet.sprite.Sprite(image, x=x, y=y, batch=batch)
        
        self.left = False
        self.right = False
        self.up = False
        self.down = False

        self.velx = 0
        self.vely = 0

        super(Player, self).__init__()

    #block of movement functions
    def start_movin_up(self):
        self.up = True
    def start_movin_down(self):
        self.down = True
    def start_movin_left(self):
        self.left = True
    def start_movin_right(self):
        self.right = True
    def stop_movin_up(self):
        self.up = False
    def stop_movin_down(self):
        self.down = False
    def stop_movin_left(self):
        self.left = False
    def stop_movin_right(self):
        self.right = False

    def update(self, dt):
        #move
        self._update_position(dt)
        #do collision stuff
        self._check_collisions()
        #do animation stuff

    def _update_position(self, dt):
        self.velx = friction * self.velx
        self.vely = friction * self.vely
        
        dax = day = 0
        if self.up:
            day += player_acc
            self.sprite.rotation = 0
        if self.down:
            day -= player_acc
            self.sprite.rotation = 180
        if self.right:
            dax += player_acc
            self.sprite.rotation = 90
        if self.left:
            dax -= player_acc
            self.sprite.rotation = 270
        self.velx += dax * dt
        self.vely += day * dt
        #if self.velx > max_speed:
            #self.velx = max_speed
        #elif self.velx < -max_speed:
            #self.velx = -max_speed
        #if self.vely > max_speed:
            #self.vely = max_speed
        #elif self.vely < -max_speed:
            #self.vely = -max_speed
        self.sprite.x += int(self.velx * dt)
        self.sprite.y += int(self.vely * dt)

    def _check_collisions(self):
        lastrect = self.rect
        self.rect = self.get_rect()
        for building in World.buildings:
            if collision(self.rect, building.rect):
                self.keep_out_of(building.rect, lastrect)
                if lastrect[1] <= building.rect[0] or lastrect[0] >= \
                    building.rect[1]:
                    #hit from the side
                    if self.velx > hit_threshold:
                        building.be_hit(self.velx)
                    self.velx = bounceback * (-self.velx)
                if lastrect[3] <= building.rect[2] or lastrect[2] >= \
                      building.rect[3]:
                    #vertical hit
                    if self.vely > hit_threshold:
                        building.be_hit(self.vely)
                    self.vely = bounceback * (-self.vely)

class Bengt(Player):
    def __init__(self, x, y):
        img = pyglet.resource.image("tanksprite.png")
        Player.__init__(self, img, x, y)

